﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.HVAC.Properties;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class RadiatorNumForm : System.Windows.Forms.Form
	{
		public RadiatorNumForm(bool setRadiatorNum)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{C25280E6-A279-48AC-9650-F573E6E60C1D}", null);
			this.setRadiatorNum = setRadiatorNum;
			this.userSetting = new HVACUserSetting();
			this.readData();
			base.Icon = Resource.YArchitechIcon;
			this.hytbNum.GotFocus += this.tbNum_Focus;
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			base.Size = new Size(Convert.ToInt32(138.0 * num), Convert.ToInt32(414.0 * num2));
		}

		private void btn5_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void btn6_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void btn7_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void btn8_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void btn9_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void btn10_Click(object sender, EventArgs e)
		{
			this.swithClick((Button)sender);
		}

		private void tbNum_Focus(object sender, EventArgs e)
		{
			this.clearButtons();
		}

		private void cbDimension_CheckedChanged(object sender, EventArgs e)
		{
			this.cbDimensionChange(((CheckBox)sender).Checked);
		}

		private void cbUp_CheckedChanged(object sender, EventArgs e)
		{
			this.IsUp = ((RadioButton)sender).Checked;
			if (this.IsUp)
			{
				this.pbDimension.Image = YArchitech.HVAC.Properties.Resources.up;
				return;
			}
			this.pbDimension.Image = YArchitech.HVAC.Properties.Resources.down;
		}

		private void hytbNum_TextChanged(object sender, EventArgs e)
		{
			this.RadiatorNum = ((YJKTextBox)sender).Text.ToInt(0);
		}

		private void readData()
		{
			if (this.setRadiatorNum)
			{
				this.chkReadRadiatorNum.Checked = (!(this.userSetting.ReadValue("RadiatorNum", "SetNumValue") == "") && Convert.ToBoolean(this.userSetting.ReadValue("RadiatorNum", "SetNumValue")));
				this.Text = "片数赋值";
			}
			else
			{
				this.chkReadRadiatorNum.Checked = (this.userSetting.ReadValue("RadiatorNum", "TagNumValue") == "" || Convert.ToBoolean(this.userSetting.ReadValue("RadiatorNum", "TagNumValue")));
				this.Text = "片数标注";
			}
			this.hytbNum.Text = ((this.userSetting.ReadValue("RadiatorNum", "Num") == "") ? "6" : this.userSetting.ReadValue("RadiatorNum", "Num"));
			this.cbDimension.Checked = (this.userSetting.ReadValue("RadiatorNum", "Dimension") == "" || Convert.ToBoolean(this.userSetting.ReadValue("RadiatorNum", "Dimension")));
			if (this.userSetting.ReadValue("RadiatorNum", "DimensionUp") == "" || Convert.ToBoolean(this.userSetting.ReadValue("RadiatorNum", "DimensionUp")))
			{
				this.cbUp.Checked = true;
				return;
			}
			this.cbDown.Checked = true;
		}

		public void WriteData()
		{
			if (this.setRadiatorNum)
			{
				this.userSetting.WriteValue("RadiatorNum", "SetNumValue", this.chkReadRadiatorNum.Checked.ToString());
			}
			else
			{
				this.userSetting.WriteValue("RadiatorNum", "TagNumValue", this.chkReadRadiatorNum.Checked.ToString());
			}
			this.userSetting.WriteValue("RadiatorNum", "Num", this.hytbNum.Text.ToString());
			this.userSetting.WriteValue("RadiatorNum", "Dimension", this.cbDimension.Checked.ToString());
			this.userSetting.WriteValue("RadiatorNum", "DimensionUp", this.cbUp.Checked.ToString());
		}

		private void cbDimensionChange(bool b)
		{
			this.IsDimension = b;
			this.cbUp.Enabled = b;
			this.cbDown.Enabled = b;
		}

		private void swithClick(Button btn)
		{
			this.clearButtons();
			btn.FlatStyle = FlatStyle.Popup;
			this.hytbNum.Text = btn.Text.Replace("片", "");
		}

		private void clearButtons()
		{
			this.btn5.FlatStyle = FlatStyle.Standard;
			this.btn6.FlatStyle = FlatStyle.Standard;
			this.btn7.FlatStyle = FlatStyle.Standard;
			this.btn8.FlatStyle = FlatStyle.Standard;
			this.btn9.FlatStyle = FlatStyle.Standard;
			this.btn10.FlatStyle = FlatStyle.Standard;
		}

		private void DisableOption()
		{
			this.btn10.Enabled = false;
			this.btn9.Enabled = false;
			this.btn8.Enabled = false;
			this.btn7.Enabled = false;
			this.btn6.Enabled = false;
			this.btn5.Enabled = false;
			this.hytbNum.Enabled = false;
		}

		private void EnabledOption()
		{
			this.btn10.Enabled = true;
			this.btn9.Enabled = true;
			this.btn8.Enabled = true;
			this.btn7.Enabled = true;
			this.btn6.Enabled = true;
			this.btn5.Enabled = true;
			this.hytbNum.Enabled = true;
		}

		private void chkReadRadiatorNum_CheckedChanged(object sender, EventArgs e)
		{
			if ((sender as CheckBox).Checked)
			{
				this.DisableOption();
				return;
			}
			this.EnabledOption();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void RadiatorNumForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.ClickESC();
		}

		private void ClickESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			if (this.setRadiatorNum)
			{
				RadiatorNumForm.SendMessage(SetRadiatorNum.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			else
			{
				RadiatorNumForm.SendMessage(RadiatorTag.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			}
			RadiatorNumForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			RadiatorNumForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void RadiatorNumForm_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.KeyPreview = true;
		}

		private void RadiatorNumForm_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				this.ClickESC();
			}
		}

		private void RadiatorNumForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public int RadiatorNum;

		public bool IsDimension = true;

		public bool IsUp = true;

		private HVACUserSetting userSetting;

		private bool setRadiatorNum;

		private ControlInfoManager m_thisManger;
	}
}
